﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Common;

namespace HanGu.CRM.Areas.SystemConfig.Controllers
{
    public class DefaultController : AdminBaseController
    {
        public ActionResult Index()
        {
            return View(GetSystemConfigList());
        }

        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {

            SearchParma.Pagination.RowCount = null;
            return View(GetSystemConfigList());
        }

        List<HanGu.Entity.SystemConfig> GetSystemConfigList()
        {
            List<HanGu.Entity.SystemConfig> systemConfigList = serviceSystemConfig.GetAll(base.SearchParma);
            ViewData["RowCount"] = SearchParma.Pagination.RowCount;
            return systemConfigList;
        }

        public ActionResult Create(string id)
        {
            HanGu.Entity.SystemConfig systemConfig = new HanGu.Entity.SystemConfig();
            if (id != null && id != "0")
            {
                systemConfig = serviceSystemConfig.GetByID(id);
                if (systemConfig != null)
                {
                    ViewData["action"] = "edit";
                }
            }
            return View(systemConfig);
        }

        //
        // POST: /SystemConfig/Create

        [HttpPost]
        [ValidateInput(false)]
        public string Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                HanGu.Entity.SystemConfig systemConfig = new HanGu.Entity.SystemConfig();
                TryUpdateModel(systemConfig, collection);

                if (Request["action"] == "edit")
                {
                    serviceSystemConfig.Update(systemConfig);
                }
                else
                {
                    serviceSystemConfig.Add(systemConfig);
                }
                System.Web.HttpContext.Current.Cache.Remove("SystemConfigAll");
                ajaxStr = string.Format(ajaxStr, 200, ResourceManager.SaveSuccess, navTabId, "", "closeCurrent");

            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.SaveFail, navTabId, "", "");
            }
            return "{" + ajaxStr + "}";
        }



        //
        // GET: /SystemConfig/Delete/5
        [HttpPost]
        public string Delete(string id, string guid)
        {
            try
            {
                if (serviceSystemConfig.Delete(id))
                {
                    ajaxStr = string.Format(ajaxStr, 200, ResourceManager.DeleteSuccess, navTabId, "", "");
                }
                else
                {
                    ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
                }
            }
            catch (Exception ex)
            {
                ExceptionHand(ex);
                ajaxStr = string.Format(ajaxStr, 300, ResourceManager.DeleteFail, navTabId, "", "");
            }

            return "{" + ajaxStr + "}";

        }
    }
}
